package net.gd.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;

import net.gd.domain.dbPermissionDomain;
import net.gd.domain.topicDetail;
import net.gd.utils.Constants;
import net.gd.utils.JdbcUtils;

public abstract class DBPermissionDAOImpl implements DBPermissionDAO {
	private static Logger logger = Logger.getLogger(DBPermissionDAOImpl.class);

	public static void main(String[] args) {
		String[] split = "sf_sd".split("_", 3);
		System.out.println(split.length);

//		DBPermissionDAO dBPermissionDAO = DAOFactory.getDBPermissionDAO();
//		String sql = "select tableName,modPM,usePM from DBPermission where userId=123 and topic='topic2'";
//
//		try {
//			dBPermissionDAO.search(sql);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String time = format.format(Calendar.getInstance().getTime());
//		System.out.println(time);

	
	}

	@Override
	public void insert(String sql, List<Object> params) throws Exception {
		JdbcUtils jdbcUtils = JdbcUtils.getInstance();
		Connection connection = jdbcUtils.getConnection();
		PreparedStatement pstmt;
		int index = 1;
		pstmt = connection.prepareStatement(sql);
		if (params != null && !params.isEmpty()) {
			for (int i = 0; i < params.size(); i++) {
				pstmt.setObject(index++, params.get(i));
			}
		}
		pstmt.executeUpdate();
		pstmt.close();
		connection.close();
	}

	public void update(String sql, HashMap<String, dbPermissionDomain> perHM) throws Exception {
		JdbcUtils jdbcUtils = JdbcUtils.getInstance();
		Connection connection = jdbcUtils.getConnection();
		connection.setAutoCommit(false);
		PreparedStatement pstmt;
		int count = 0;
		pstmt = connection.prepareStatement(sql);
		for (Iterator<Map.Entry<String, dbPermissionDomain>> iter = perHM
				.entrySet().iterator(); iter.hasNext();) {
			Map.Entry<String, dbPermissionDomain> entry = (Entry<String, dbPermissionDomain>) iter
					.next();
			String dbTable = (String) entry.getKey();
			String[] split = dbTable.split("_",2);
			if(2 == split.length){
				dbPermissionDomain permision = (dbPermissionDomain) entry.getValue();
				pstmt.setString(1, split[0]);
				pstmt.setString(2, split[1]);
				pstmt.setString(3, permision.getTopicid());
				pstmt.addBatch();

				count++;
			}
			
		}
		pstmt.executeBatch();
		connection.commit();
		logger.info(Thread.currentThread().getName()+" update DBPermission  success:"
				+ count);
		pstmt.clearBatch();
		pstmt.close();
		connection.close();
	}
	
	@Override
	public void updateTopic(String sql, HashMap<String, topicDetail> topic) throws Exception {
		JdbcUtils jdbcUtils = JdbcUtils.getInstance();
		Connection connection = jdbcUtils.getConnection();
		connection.setAutoCommit(false);
		PreparedStatement pstmt;
		int count = 0;
		pstmt = connection.prepareStatement(sql);
		for (Iterator<Map.Entry<String, topicDetail>> iter = topic
				.entrySet().iterator(); iter.hasNext();) {
			Entry<String, topicDetail> entry = (Entry<String, topicDetail>) iter
					.next();
			String topicName = (String) entry.getKey();
			topicDetail single = (topicDetail) entry.getValue();
//			pstmt.setString(1, single.getRoleid());
			pstmt.setString(2, topicName);
			pstmt.setString(3, single.getTopictype());
			pstmt.addBatch();
			count++;
			
		}
		pstmt.executeBatch();
		connection.commit();
		logger.info(Thread.currentThread().getName()+" update DBPermission  success:"
				+ count);
		pstmt.clearBatch();
		pstmt.close();
		connection.close();
	}
	
	@Override
	public void inserPermission(String sql, List<dbPermissionDomain> perList) throws Exception{
		JdbcUtils jdbcUtils = JdbcUtils.getInstance();
		Connection connection = jdbcUtils.getConnection();
		connection.setAutoCommit(false);
		PreparedStatement pstmt;
		int count = 0;
		pstmt = connection.prepareStatement(sql);
		for (dbPermissionDomain per:perList) {
			//topicId,dbName,tableName,rdPM,wtPM
				pstmt.setString(1, per.getTopicid());
				pstmt.setString(2, per.getDbname());
				pstmt.setString(3, per.getTablename());
				pstmt.setInt(4, per.getRdpm());
				pstmt.setInt(5, per.getWtpm());
				pstmt.addBatch();
				count++;
		}
		pstmt.executeBatch();
		connection.commit();
		logger.info(Thread.currentThread().getName()+"insert DBPermission  success:"
				+ count);
		pstmt.clearBatch();
		pstmt.close();
		connection.close();
	}
	@Override
	public Map<String, Object> search(String sql) throws Exception {
		Map<String, Object> dbInfoMap = new HashMap<String, Object>();
		JdbcUtils jdbcUtils = JdbcUtils.getInstance();
		Connection connection = jdbcUtils.getConnection();
		PreparedStatement pstmt;
		connection.setAutoCommit(false);
		ResultSet rs = null;
		pstmt = connection.prepareStatement(sql);
		rs = pstmt.executeQuery(sql);
		
		dbInfoMap.put(Constants.CONNECTION, connection);
		dbInfoMap.put(Constants.PREPARED_STATEMENT, pstmt);
		dbInfoMap.put(Constants.RESULT_SET, rs);
		return dbInfoMap;
	}
	
	@Override
	public void deleteTopic(String sql) throws SQLException{
		JdbcUtils jdbcUtils = JdbcUtils.getInstance();
		Connection connection = jdbcUtils.getConnection();
		PreparedStatement pstmt;
		pstmt = connection.prepareStatement(sql);
		pstmt.execute(sql);
		logger.info(Thread.currentThread().getName()+"delete topicDetail  success:");
		pstmt.close();
		connection.close();
	}
	
	@Override
	public Map<String, Object> selectRoles(String sql) throws SQLException{
		Map<String, Object> dbInfoMap = new HashMap<String, Object>();
		JdbcUtils jdbcUtils = JdbcUtils.getInstance();
		Connection connection = jdbcUtils.getConnection();
		PreparedStatement pstmt;
		pstmt = connection.prepareStatement(sql);
		ResultSet rs = null;
		pstmt = connection.prepareStatement(sql);
		rs = pstmt.executeQuery(sql);
		dbInfoMap.put(Constants.CONNECTION, connection);
		dbInfoMap.put(Constants.PREPARED_STATEMENT, pstmt);
		dbInfoMap.put(Constants.RESULT_SET, rs);
		logger.info(Thread.currentThread().getName()+"select current roles and topicName ssuccess:");
		return dbInfoMap;
	}
	
}
